import 'package:dio/dio.dart';
import 'package:mall/core/services/auth.dart';
import 'package:mall/core/providers/view_model.dart';

class UserNicknameUpdateModel extends ViewModel {
  final Auth auth;
  String newNickname;
  String _errorMessage;

  UserNicknameUpdateModel(this.auth) : super();

  String get errorMessage => _errorMessage;

  Future<ViewState> userNicknameUpdate() async {
    viewState = ViewState.busy;
    try {
      if (newNickname == null || newNickname.isEmpty) {
        throw '请输入昵称';
      }
      await auth.userNicknameUpdate(newNickname);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
